package com.zjdiepu.www.manage.api.partner;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.ThirdBusiStatus;
import com.zjdiepu.www.base.enums.type.AttachmentSort;
import com.zjdiepu.www.base.enums.type.AttachmentType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.base.IOtherAttachmentsService;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleBankService;
import com.zjdiepu.www.ibiz.file.IQiniuFileService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.OtherAttachments;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 鑫合汇相关操作Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年08月30日 14时09分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "YwXinHeHuiController", produces="application/json;charset=UTF-8")
@Api(tags = {"鑫合汇相关操作"}, produces="application/json;charset=UTF-8")
public class YwXinHeHuiController extends BaseController {
    @Resource
    private IOtherAttachmentsService otherAttachmentsService;
    @Resource
    private IQiniuFileService qiniuFileService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private IBaseBusiUnitService baseBusiUnitService;
    @Resource
    private IDzLoanMiddleBankService dzloanMiddleBankService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    
    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
        PageVo page = getPage(ywBusinessQueryRequest);
        SysUser sysUser = (SysUser)getCurrentUser();
        ywBusinessQueryRequest.setOrderSql(" B.created desc");
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(sysUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(sysUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        ywBusinessQueryRequest.setDh(true);
        ywBusinessQueryRequest.setThirdBusiStatusLess(ThirdBusiStatus.CHU_SHEN_ZHONG.getCode());
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
        	String orgCode="";
        	BaseBusiUnit baseBusiUnit=null;
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            for (YwBusiness ywBusiness : ywBusinesss) {
            	baseBusiUnit=new BaseBusiUnit();
            	orgCode=ywBusiness.getOrgCode().substring(0,4);
            	baseBusiUnit.setOrgCode(orgCode);
            	baseBusiUnit=baseBusiUnitService.selectRefined(baseBusiUnit);
            	if(baseBusiUnit!=null){
            		ywBusiness.setFgsName(baseBusiUnit.getOrgName());
            	}
			}
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
	
	/**
	 * 导出鑫合汇打包资料
	 * @author xiongxiaotun <br/>
	 * @Title: downloadRar  <br/>
	 * @Description: 导出鑫合汇打包资料 <br/>
	 * @return
	 * @version V1.0  <br/>
	 */
	@RequestMapping(value = "downloadRar", method = {RequestMethod.POST} )
	public ApiResponse downloadRar(HttpServletResponse response, String busiId) {
		getCurrentUser();
		if(StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow("999", "参数业务ID不能为空");
		}
		OtherAttachments otherAttachments = new OtherAttachments();
		otherAttachments.setDataid(busiId);
		otherAttachments.setDpStatus(DpStatus.NORMAL.getCode());
		otherAttachments.setAttachmentSort(AttachmentSort.XHH_FU_JIAN_DA_BAO.getCode());
		otherAttachments.setAttachmentType(AttachmentType.RAR.getCode());
		otherAttachments = otherAttachmentsService.selectRefined(otherAttachments);
		if(null == otherAttachments) {
			ExceptionHelper.thow("999", "鑫合汇附件打包记录不存在或已被删除");
		}
		String ossPath = otherAttachments.getOssPath();
		if(!qiniuFileService.exists(ossPath)){
			ExceptionHelper.thow("999", "鑫合汇附件打包文件不存在或已被删除");
		}
		return ApiResponse.success(qiniuFileService.getURL(ossPath));
	}
}
